﻿using AudioStudio;
using AudioStudio.Components;
using AudioStudio.Tools;
using GameFramework.Battle.Core;
using System;
using System.Collections.Generic;
using com.yoozoo.gta.Extension;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.RTS
{
    public class TrainEffectInfo
    {
        public GameObject effect;
        public GameObject effectObj1;
        public GameObject effectObj2;
        public GameObject effectObj3;
    }
    
    class TradeTrainModel : SquareModel
    {
        private Animation _animation; // 列车模型动画组件
        private TrainEffectInfo _trainEffectInfo;
        
        // 列车驶出奇观 展开动画
        public void OnMoveInWonder(float aniPro)
        {
            GetAnimation();
            if (_animation)
            {
                string ani_in_name = "ani_trade_train_in";
                float allTime = 1.333333f;
                AnimationState ani_state = _animation[ani_in_name];
                ani_state.time = allTime * aniPro;
                ani_state.speed = 1; /// MarchManager.Instance.trainTestAniSpeed;
                _animation.Play(ani_in_name);
            }
        }

        // 列车驶入奇观 折叠动画
        public void OnMoveOutWonder(float aniPro)
        {
            GetAnimation();
            if (_animation)
            {
                string ani_out_name = "ani_trade_train_out";
                float allTime = 1.333333f;
                AnimationState ani_state = _animation[ani_out_name];
                ani_state.time = allTime * aniPro;
                ani_state.speed = 1;// / MarchManager.Instance.trainTestAniSpeed;
                _animation.Play(ani_out_name);
            }
        }

        // 捕获一下节点
        public void GetAnimation()
        {
            if (_animation == null || !_animation.Valid())
            {
                _animation = GameObject.GetComponentInChildren<Animation>();
            }
        }

        public void SetAnimation(bool value)
        {
            GetAnimation();
            if (_animation)
            {
                _animation.enabled = value;
            }
        }

        // 更新着火特效显示
        public void UpdateFireShow(bool bShow,int fireNum,Vector3 forward)
        {
            if (bShow && _trainEffectInfo == null)
            {
                Transform aniTf = this.GameObject.transform.GetChild(0);
                if (aniTf)
                {
                    var effect = ArmyManager.GetResourceById(MarchHelper.TrainEffect);
                    effect.transform.SetParent(aniTf);
                    effect.transform.localPosition = Vector3.zero;
                    TrainEffectInfo info = new TrainEffectInfo();
                    info.effect = effect;
                    info.effectObj1 = effect.transform.Find("1").gameObject;
                    info.effectObj2 = effect.transform.Find("2").gameObject;
                    info.effectObj3 = effect.transform.Find("3").gameObject;
                    _trainEffectInfo = info;
                }
            }

            if (_trainEffectInfo != null)
            {
                TrainEffectInfo info = _trainEffectInfo;
                info.effectObj1.SetActive(fireNum > 0);
                info.effectObj2.SetActive(fireNum > 1);
                info.effectObj3.SetActive(fireNum > 2);
                info.effect.transform.forward = forward;
                info.effect.SetActive(bShow);
            }
        }

        // 因为列车的碰撞体要转向
        public CapsuleCollider GetCollider()
        {
            CapsuleCollider collider = GameObject.GetComponentInChildren<CapsuleCollider>();
            return collider;
        }

        public void Dispose()
        {
            if (_trainEffectInfo != null)
            {
                ArmyManager.RecoverResourceById(MarchHelper.TruckEffect,_trainEffectInfo.effect);
                _trainEffectInfo = null;
            }

            CapsuleCollider collider = GetCollider();
            if (collider)
            {
                collider.name = "colliderTrain";
            }
        }

        protected override void OnDead()
        {
            
        }
        
        public override void SetForward(Vector3 forward)
        {
            if (GameObject)
            {
                GameObject.transform.forward = forward;
            }
        }
    }
}
